package com.wthink.hzgj.camera

import android.os.Bundle
import android.view.LayoutInflater
import android.view.SurfaceHolder
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.viewModels
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.wthink.hzgj.R
import com.wthink.hzgj.camera.adapter.CameraAdapter
import com.wthink.hzgj.camera.data.Camera
import com.wthink.hzgj.camera.viewmodel.CameraViewModel
import com.wthink.hzgj.databinding.FragmentCameraBinding
import com.wthink.hzgj.util.NoBackFragment
import com.wthink.hzgj.util.adapter.OnItemClickListener
import kotlinx.android.synthetic.main.fragment_camera.*


class CameraFragment : NoBackFragment() {

    private val viewModel by viewModels<CameraViewModel>()
    private lateinit var binding: FragmentCameraBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_camera,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this

        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        binding.cameraListView.layoutManager = FlexboxLayoutManager(context).apply {
            justifyContent = JustifyContent.FLEX_END
        }
        binding.cameraListView.adapter = CameraAdapter().apply {
            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    viewModel.select(obj as Camera)
                }
            }
        }

        binding.cameraListView.visibility = View.GONE
        cameraView.holder.addCallback(object : SurfaceHolder.Callback {
            private var mCamera: android.hardware.Camera? = null
            private fun getDefaultCamera(): Int {
                val numberOfCameras = android.hardware.Camera.getNumberOfCameras()
                val cameraInfo = android.hardware.Camera.CameraInfo()

                var defaultCameraId = 0

                for (i in 0 until numberOfCameras) {
                    android.hardware.Camera.getCameraInfo(i, cameraInfo)
                    if (cameraInfo.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK) {
                        defaultCameraId = i
                        break
                    }
                }
                return defaultCameraId
            }

            override fun surfaceChanged(
                holder: SurfaceHolder?,
                format: Int,
                width: Int,
                height: Int
            ) {
            }

            override fun surfaceDestroyed(holder: SurfaceHolder?) {
                mCamera?.stopPreview()
                mCamera?.release()
                mCamera = null
            }

            override fun surfaceCreated(holder: SurfaceHolder?) {
                mCamera = android.hardware.Camera.open(getDefaultCamera())
                mCamera!!.apply {
//                    val parameters = getParameters()
//                    parameters.setPreviewSize(
//                        binding.cameraListView.width,
//                        binding.cameraListView.height
//                    )
                    setPreviewDisplay(holder)
                    startPreview()
                }
            }
        })
        viewModel.start()
    }
}
